/**
 * 描述: 服务内容详情页
 *
 * @author fengxuecai
 * @since 2021/11/17 14:19
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;

    // 初始化表單
    layui.use("form", function () {
        var acceTypeValue = $("input[name=acceType]:checked").val()
        if (acceTypeValue == 2) {
            // 用户验收 隐藏参考值 选项值
            $("#referenceValue").hide();
            $("#selectValue0").hide();
            $("#selectValue1").hide();
            $("#selectValue2").hide();
            $("#remark").hide();
            $("input[name=referenceValue]").attr("lay-verify", "");
            $("input[name=selectValue0]").attr("lay-verify", "");
            $("input[name=selectValue1]").attr("lay-verify", "");
            $("input[name=selectValue2]").attr("lay-verify", "");
            form.render(null, "zmn-form");
        }
    })
    // 自定义验证规则
    form.verify({
        sort: function (value) {
            if (value && !/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
            if (value.length > 10) {
                return "排序值不能大于10位正整数";
            }
        }
    });
    // 第三选项值和单选框监听
    form.on('radio(isNormal)', function (data) {
        //得到radio原始DOM对象 console.log(data.elem);
        //被点击的radio的value值 console.log(data.value);
        var level = data.value;
        if (level == 3) {
            var selectValue2 = $('input[name="selectValue2"]').val()
            if (selectValue2 == "" || selectValue2 == undefined) {
                layer.msg("无法选中空选项值", {"icon": 2});
                $("input[name=isNormal]:checked").prop("checked", false);
                $("input[name=isNormal]")[0].checked = true;
                form.render("radio");
            }
        }
    });
    $(function () {
        //输入框的值改变时触发
        $("input[name=selectValue2]").on("input", function (e) {
            var selectValue2 = e.delegateTarget.value
            if (selectValue2 == "" || selectValue2 == undefined) {
                var level = $("input[name=isNormal]:checked").val();
                if (level == 3) {
                    layer.msg("无法选中空选项值", {"icon": 2});
                    $("input[name=isNormal]:checked").prop("checked", false);
                    $("input[name=isNormal]")[0].checked = true;
                    form.render("radio");
                }
            }
        });
    });
    // 验收项目类型监听
    form.on('radio(acceType)', function (data) {
        const value = data.value;
        if (value == 2) {
            // 用户验收 隐藏参考值 选项值
            $("#referenceValue").hide();
            $("#selectValue0").hide();
            $("#selectValue1").hide();
            $("#selectValue2").hide();
            $("#remark").hide();
            $("input[name=referenceValue]").attr("lay-verify", "");
            $("input[name=selectValue0]").attr("lay-verify", "");
            $("input[name=selectValue1]").attr("lay-verify", "");
            $("input[name=selectValue2]").attr("lay-verify", "");
        }
        if (value == 1) {
            // 工程師驗收 展示参考值 选项值
            $("#referenceValue").show();
            $("#selectValue0").show();
            $("#selectValue1").show();
            $("#selectValue2").show();
            $("#remark").show();
            $("input[name=referenceValue]").attr("lay-verify", "required");
            $("input[name=selectValue0]").attr("lay-verify", "required");
            $("input[name=selectValue1]").attr("lay-verify", "required");
            $("input[name=selectValue2]").attr("lay-verify", "required");
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        var data = obj.field;
        var isNormal = $('input[name="isNormal"]:checked').val();
        var acceType = $('input[name="acceType"]:checked').val();
        if (acceType == 1) {
            // 用户验收 展示参考值 选项值
            var selectValueArr = [];
            if (obj.field.selectValue0 != "" && obj.field.selectValue0 != undefined) {
                selectValueArr.push({"value": 1, "text": obj.field.selectValue0,"status":isNormal==1});
            }
            if (obj.field.selectValue1 != "" && obj.field.selectValue1 != undefined) {
                selectValueArr.push({"value": 2, "text": obj.field.selectValue1,"status":isNormal==2});
            }
            if (obj.field.selectValue2 != "" && obj.field.selectValue2 != undefined) {
                selectValueArr.push({"value": 3, "text": obj.field.selectValue2,"status":isNormal==3});
            }
            var selectValue = JSON.stringify(selectValueArr);
            data.selectValue = selectValue;
            if (checkSelectValue(selectValueArr)) {
                layer.msg("选项值重复", {"icon": 2});
                return false;
            }
        }
        var url = "/base/acceptproject/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (result) {
                if (result.status == 200) {
                    //提示信息为弹出框形式
                    layer.msg(result.message, {time: 1000}, () => {
                        zmnLayUtil.closeThisIframe(parent, {reloadPage: true});
                        parent.window.document.forms[0].submit();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        })
        return false;
    });
    //对外暴露的接口
    exports('plat_base_acceptproject_detail', {});
});

// 选项值验重
function checkSelectValue(selectValueArr) {
    var hash = {};
    for (const selectValue of selectValueArr) {
        var text = selectValue.text;
        if (text == "" || text == undefined) {
            continue;
        }
        if (hash[text]) {
            return true;
        }
        hash[text] = true;
    }
    return false;
}
